package com.android;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TimePicker;

import com.android.data.ServerManager;
import com.android.entities.Category;

public class _ConfigurationActivity extends ListActivity {
	
	private ArrayList<String> categoriesList;
	private ProgressDialog m_ProgressDialog = null;
	private Runnable viewCategories;
	private static Thread categoriesThread;
	private CategoriesAdapter mAdapter = null;	 
	ArrayList<Category> listCategories = new ArrayList<Category>();
	
	private TimePicker timePicker;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.configuration);
        
        categoriesList = new ArrayList<String>();
        
        //timePicker = (TimePicker) findViewById(R.id.initialTime);
        timePicker.setIs24HourView(true);
        
        this.mAdapter = new CategoriesAdapter(this, R.layout.row_category, listCategories);
        setListAdapter(this.mAdapter);

        viewCategories = new Runnable(){        
            public void run() {
            	retrieveCategories();
            }
        };
	    
        categoriesThread =  new Thread(null, viewCategories, "MagentoBackground");
        categoriesThread.start();
	        m_ProgressDialog = ProgressDialog.show(_ConfigurationActivity.this,    
	              "Please wait...", "Retrieving data ...", true);
	        
	    //Button createSchedule = (Button) findViewById(R.id.createSchedule);
	    /*createSchedule.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(mContext, _ProgrammingActivity.class);
								
				i.putExtra("hour", timePicker.getCurrentHour());
				i.putExtra("minute", timePicker.getCurrentMinute());
				i.putExtra("categories", categoriesList);
				
				mContext.startActivity(i);
			}
	    });*/
    }
    
    private void retrieveCategories() {
    	try{
    		listCategories = ServerManager.getCategories(_ConfigurationActivity.this);
    		    		    		
    	} catch (Exception e) {
    		if (e.getMessage() != null)
    			android.util.Log.e("BACKGROUND_PROC", e.getMessage());
            e.printStackTrace();
    	}
    	runOnUiThread(returnRes);	
    }
    
    private Runnable returnRes = new Runnable() {

        public void run() {
            if(listCategories != null && listCategories.size() > 0){
            	mAdapter.notifyDataSetChanged();
            	
                for(int i = 0; i < listCategories.size(); i++)
                	mAdapter.add(listCategories.get(i));
            }
            
            m_ProgressDialog.dismiss();
            mAdapter.notifyDataSetChanged();
        }
    };
    
    private class CategoriesAdapter extends ArrayAdapter<Category> {

        private ArrayList<Category> items;
        private Context mContext;
        
        public CategoriesAdapter(Context context, int textViewResourceId, ArrayList<Category> items) {
            super(context, textViewResourceId, items);
            this.items = items;
            this.mContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        	View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row_category, null);
            }
            
            final Category category  = items.get(position);
            if (category != null) {
            	final CheckBox title = (CheckBox) v.findViewById(R.id.categoryName);
                if (title != null)
                      title.setText(category.getName());
                
                title.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (title.isChecked())
							addCategory(category.getId());
						else
							removeCategory(category.getId());
					}
                });
            }
            
            return v;
        }
        
    }
    
    private void addCategory(String categoryId) {
    	categoriesList.add(categoryId);
    }
    
    private void removeCategory(String categoryId) {
    	categoriesList.remove(categoryId);
    }
}
